package com.ruoyi.ruoyi.business.service.smo.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.pojo.entity.AppUser;
import com.ruoyi.ruoyi.business.pojo.dto.MiniUserEditGradeDto;
import com.ruoyi.ruoyi.business.pojo.dto.MiniUserEditStateDto;
import com.ruoyi.ruoyi.business.pojo.dto.MiniUserListDto;
import com.ruoyi.ruoyi.business.pojo.vo.MiniUserListVo;
import com.ruoyi.ruoyi.business.service.smo.MiniUserService;
import com.ruoyi.system.service.AppUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * ClassName：MiniUserServiceImpl
 * Package：com.ruoyi.ruoyi.business.service.smo.impl
 * Description：
 *
 * @Author：fpy
 * @Create：2025/5/8 - 16:08
 * @Version：v1.0
 */
@Service
public class MiniUserServiceImpl implements MiniUserService {
    @Resource
    private AppUserService appUserService;

    @Override
    public AjaxResult<Page<MiniUserListVo>> list(Page dtoPage, MiniUserListDto dto) {
        Page<AppUser> page = appUserService.page(dtoPage,
                Wrappers.<AppUser>lambdaQuery()
                        .eq(dto.getGrade() != null, AppUser::getGrade, dto.getGrade())
                        .like(StrUtil.isNotBlank(dto.getPhone()), AppUser::getMobile, dto.getPhone())
                        .orderByDesc(AppUser::getCreateTime)
        );

        Page<MiniUserListVo> pageVo = new Page<>();
        pageVo.setTotal(page.getTotal());
        pageVo.setSize(page.getSize());
        pageVo.setCurrent(page.getCurrent());
        pageVo.setRecords(
                Optional.ofNullable(page.getRecords())
                        .orElseGet(ArrayList::new)
                        .stream()
                        .map(mu -> {
                            MiniUserListVo vo = new MiniUserListVo();
                            vo.setId(mu.getId());
                            vo.setName(mu.getNickName());
                            vo.setPhone(mu.getMobile());
                            vo.setGrade(mu.getGrade());
                            vo.setRegistration_time(mu.getCreateTime());
                            vo.setState(mu.getStatus());

                            return vo;
                        })
                        .collect(Collectors.toList())
        );

        return AjaxResult.success(pageVo);
    }

    @Override
    public AjaxResult editState(MiniUserEditStateDto dto) {
        AppUser appUser = new AppUser();
        appUser.setId(dto.getId());
        appUser.setStatus(dto.getState());
        appUserService.updateById(appUser);

        return AjaxResult.success();
    }

    @Override
    public AjaxResult editGrade(MiniUserEditGradeDto dto) {
        AppUser appUser = new AppUser();
        appUser.setId(dto.getId());
        appUser.setGrade(dto.getGrade());
        appUserService.updateById(appUser);

        return AjaxResult.success();
    }
}
